home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / fg / fgmisc10 / areas.c < prev    next >
Text File  |  1993-10-25  |  7KB  |  253 lines

  1. /****************************************************************************\
  2. *                                                                            *
  3. *  AREAS.C                                                                   *
  4. *                                                                            *
  5. *  Copyright 1993 Diana Gruber. All rights reserved.                         *
  6. *                                                                            *
  7. *  This is example source code for Fastgraph users. This is just a simple    *
  8. *  that demonstrates how to use the mouse. The mouse touches areas on the    *
  9. *  screen and reports the color.                                             *
  10. *                                                                            *
  11. *  The code is interesting because it uses an array of structures. Okay,     *
  12. *  maybe it's not that interesting, but it is free. :)                       *
  13. *                                                                            *
  14. *  You may use any or all of this code in your Fastgraph applications.       *
  15. *                                                                            *
  16. *  See MISC.DOC for compile and link commands.                               *
  17. *                                                                            *
  18. \****************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <fastgraf.h>
  24.  
  25. #define BETWEEN(x,a,b) ((x >= a) && (x <= b))
  26. #define ESC  27
  27. #define FALSE 0
  28. #define TRUE  1
  29.  
  30. /* be sure to declare functions before defining structures */
  31.  
  32. int looking_at(void);
  33. int touching(void);
  34. int do_areas(void);
  35. int default_message(void);
  36.  
  37. /* pointer to an integer function */
  38.  
  39. typedef int (*PFI)();
  40.  
  41. /* define the structure */
  42.  
  43. typedef struct cmd
  44. {
  45.    PFI  func;        /* function to be called */
  46.    char *string;     /* the menu item as written on the screen */
  47.    int x1;           /* coordinates of active menu area */
  48.    int x2;
  49.    int y1;
  50.    int y2;
  51. }  CMD;
  52.  
  53. /* declare some structures */
  54.  
  55. int NAREAS = 4;
  56.  
  57. CMD mouse_area[] =
  58. {
  59.    looking_at, "RED",     0, 99, 16,199,
  60.    looking_at, "BLUE",  100,199, 16,199,
  61.    touching,   "GREEN", 200,299, 16, 99,
  62.    touching,   "YELLOW",200,299,100,199
  63. };
  64.  
  65. char area_string[80];
  66.  
  67. /****************************************************************************\
  68. *                                                                            *
  69. *  main                                                                      *
  70. *                                                                            *
  71. \****************************************************************************/
  72.  
  73. void main()
  74. {
  75.    int old_mode;
  76.  
  77.    /* initialize the video environment and the mouse */
  78.  
  79.    if (fg_testmode(19,0) == 0)
  80.    {
  81.       printf("This program requires VGA or MCGA graphics.\n");
  82.       exit(1);
  83.    }
  84.    old_mode = fg_getmode();
  85.    fg_setmode(19);
  86.  
  87.    /* initialize the mouse */
  88.  
  89.    if (fg_mouseini() <= 0)
  90.    {
  91.       fg_setmode(old_mode);
  92.       fg_reset();
  93.       printf("This program requires a mouse.\n");
  94.       exit(1);
  95.    }
  96.  
  97.    do_areas();
  98.  
  99.    /* reset the video mode and exit */
  100.  
  101.    fg_setmode(old_mode);
  102.    fg_reset();
  103.    exit(0);
  104. }
  105.  
  106. /****************************************************************************\
  107. *                                                                            *
  108. *  do_areas -- display information based on what are the mouse is touching   *
  109. *                                                                            *
  110. \****************************************************************************/
  111.  
  112. int do_areas()
  113. {
  114.    unsigned char key,aux;
  115.    register int i;
  116.    int current_area;
  117.    int xmouse,ymouse,buttons;
  118.    int x1,x2,y1,y2;
  119.    int found;
  120.    static int area_color[] = {4,1,2,14};
  121.  
  122.    /* draw some colored rectangles */
  123.  
  124.    for (i = 0; i < NAREAS; i++)
  125.    {
  126.       x1 = mouse_area[i].x1;
  127.       x2 = mouse_area[i].x2;
  128.       y1 = mouse_area[i].y1;
  129.       y2 = mouse_area[i].y2;
  130.  
  131.       fg_setcolor(area_color[i]);
  132.       fg_rect(x1,x2,y1,y2);
  133.    }
  134.    fg_mousevis(1);
  135.  
  136.    /* now look at where the mouse is */
  137.  
  138.    current_area = -1;
  139.    for(;;)
  140.    {
  141.       fg_mousepos(&xmouse,&ymouse,&buttons);
  142.       found = FALSE;
  143.       for (i = 0; i < NAREAS; i++)
  144.       {
  145.          x1 = mouse_area[i].x1;
  146.          x2 = mouse_area[i].x2;
  147.          y1 = mouse_area[i].y1;
  148.          y2 = mouse_area[i].y2;
  149.  
  150.          if (BETWEEN(xmouse,x1,x2) && BETWEEN(ymouse,y1,y2))
  151.          {
  152.             found = TRUE;
  153.  
  154.             /* have we moved into a new area? */
  155.  
  156.             if (i != current_area)
  157.             {
  158.                strcpy(area_string,mouse_area[i].string);
  159.  
  160.                /* call the function as defined in the structure */
  161.  
  162.                (*mouse_area[i].func)();
  163.                current_area = i;
  164.                break;
  165.             }
  166.          }
  167.       }
  168.  
  169.       /* not a hot spot -- display null message */
  170.  
  171.       if (!found && current_area != NAREAS)
  172.       {
  173.          default_message();
  174.          current_area = NAREAS;
  175.       }
  176.  
  177.       /* check for ESC key */
  178.  
  179.       fg_waitfor(3);
  180.       fg_intkey(&key,&aux);
  181.       if (key == ESC) break;
  182.    }
  183.    return(0);
  184. }
  185.  
  186. /****************************************************************************\
  187. *                                                                            *
  188. *  looking_at -- function called when looking at a color                     *
  189. *                                                                            *
  190. \****************************************************************************/
  191.  
  192. looking_at()
  193. {
  194.    char string[80];
  195.    int nchar;
  196.  
  197.    strcpy(string,"I'm looking at ");
  198.    strcat(string,area_string);
  199.    nchar = strlen(string);
  200.  
  201.    fg_mousevis(0);
  202.    fg_setcolor(0);
  203.    fg_rect(0,319,0,15);
  204.    fg_setcolor(15);
  205.    fg_locate(0,5);
  206.    fg_text(string,nchar);
  207.    fg_mousevis(1);
  208.  
  209.    return(0);
  210. }
  211.  
  212. /****************************************************************************\
  213. *                                                                            *
  214. *  touching -- function called when touching a color                         *
  215. *                                                                            *
  216. \****************************************************************************/
  217.  
  218. touching()
  219. {
  220.    char string[80];
  221.    int nchar;
  222.  
  223.    strcpy(string,"I'm touching ");
  224.    strcat(string,area_string);
  225.    nchar = strlen(string);
  226.  
  227.    fg_mousevis(0);
  228.    fg_setcolor(0);
  229.    fg_rect(0,319,0,15);
  230.    fg_setcolor(15);
  231.    fg_locate(0,5);
  232.    fg_text(string,nchar);
  233.    fg_mousevis(1);
  234.  
  235.    return(0);
  236. }
  237.  
  238. /****************************************************************************\
  239. *                                                                            *
  240. *  default_message -- blank message                                          *
  241. *                                                                            *
  242. \****************************************************************************/
  243.  
  244. default_message()
  245. {
  246.    fg_mousevis(0);
  247.    fg_setcolor(0);
  248.    fg_rect(0,319,0,15);
  249.    fg_mousevis(1);
  250.  
  251.    return(0);
  252. }
  253.